<template>
  <van-cell class="comment-item">
    <van-image
      slot="icon"
      class="avatar"
      round
      fit="cover"
      :src="articleCommentItem.aut_photo"
    />
    <div
      slot="title"
      class="title-wrap"
    >
      <div class="user-name">{{articleCommentItem.aut_name}}</div>
      <van-button
        class="like-btn"
        :icon="articleCommentItem.is_liking ? 'good-job':'good-job-o'"
        :class="{liked:articleCommentItem.is_liking}"
        @click="likedRelativeOpt"
      >{{articleCommentItem.like_count||'点赞'}}</van-button>
    </div>

    <div slot="label">
      <p class="comment-content">{{articleCommentItem.content}}</p>
      <div class="bottom-info">
        <span
          class="comment-pubdate">{{articleCommentItem.pubdate|dateFormat}}</span>
        <van-button
          class="reply-btn"
          round
          @click="$emit('reply-comment',articleCommentItem)"
        >回复 {{articleCommentItem.reply_count}}</van-button>
      </div>
    </div>
  </van-cell>
</template>

<script>
// 导入对评论点赞或取消点赞的接口模块
import { addArticleLiked, cannelArticleLiked } from '../../../api/comment.js'
export default {
  name: 'articleCommentItem',
  components: {},
  props: {
    // 接收父组件传递过来的文章评论的列表项
    articleCommentItem: {
      type: Object,
      required: true
    }
  },
  data () {
    return {}
  },
  computed: {},

  watch: {},
  created () { },
  mounted () { },
  methods: {
    // 文章评论点赞相关的操作
    async likedRelativeOpt () {
      try {
        //  判断is_liking的状态
        if (this.articleCommentItem.is_liking) {
          // 取消点赞的接口
          await cannelArticleLiked(this.articleCommentItem.com_id.toString())
          this.articleCommentItem.like_count--
        } else {
          // 点赞的接口
          await addArticleLiked(this.articleCommentItem.com_id.toString())
          this.articleCommentItem.like_count++
        }
        this.articleCommentItem.is_liking = !this.articleCommentItem.is_liking
      } catch (error) {
        this.$toast('操作失败! 请稍后再试!', error)
      }
    }
  }
}
</script>

<style scoped lang="less">
.liked {
  color: orange !important;
}
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
  }
}
</style>
